/*
 * WebServer.h
 *
 *  Created on: 16/06/2014
 *      Author: Vitor
 */

#ifndef WEBSERVER_H_
#define WEBSERVER_H_

#include "TCPSocket.h"
#include "Thread.h"
#include <stdio.h>
#include <stdlib.h>
#include "HttpRequest.h"
#include "HttpResponse.h"
#include "Globals.h"
#include "File.h"
#include "PageBuilder.h"

#include "PostHandler.h"
#include "ByteQueue.h"
#include "MessageTypes.h"
#include "Balancer.h"

class WebServer {
private:
	TCPSocket welcome_socket;
	IPaddress server_ip;

	static void handle_connection(TCPSocket client_socket);
	static void handle_new_file(std::string cur_dir, std::string file_name,
			std::vector<char>& file_contents);
	static std::string convert_ip_to_string(unsigned int ip);
	static void send_file(std::string full_file_name,
			std::vector<char>& file_contents, TCPSocket socket);
	static void send_table();
	static std::string put_slash(std::string name);
	static bool handle_login(User user);
	static bool handle_cadastro(User user);
	static void handle_new_folder(std::string cur_dir, std::string new_folder);
	static void handle_rename_folder(std::string cur_dir, std::string old_name,
			std::string new_name);
	static void handle_rename_file(std::string cur_dir, std::string old_name,
			std::string new_name);
	static void handle_delete_folder(std::string, std::string);
	static void handle_delete_file(std::string, std::string);

	static std::string url_decode(const std::string& source);

	static void broadcast_to_peers(const std::vector<char>& message,
			bool including_myself = true);

public:
	WebServer(int port);
	virtual ~WebServer();

	void run();
};

#endif /* WEBSERVER_H_ */
